"use strict";

import Vue from "vue";
import axios from "axios";
import router from "../router/index.js";
import { Notification } from "element-ui";
// import store from "@/utils/store";

axios.defaults.headers.post["Content-Type"] = "application/json";
axios.defaults.withCredentials = true;

let config = {
    onErrTip: true, //默认提示业务错误信息
    // baseURL: "http://192.168.45.202:8080/online-demo", // api 的 base_url
    baseURL: "/online-demo", // api 的 base_url
    timeout: 1200000, // 请求超时时间
    // withCredentials: true, // Check cross-site Access-Control
};

const tempAxios = axios.create(config);

tempAxios.interceptors.request.use(
    function (config) {
        // 请求发出前
        return config;
    },
    function (error) {
        // 请求错误时
        return Promise.reject(error);
    },
);

// 拦截器
tempAxios.interceptors.response.use(
    function (response) {
        if (response.config.onErrTip) {
            doBusErrCode(response); //处理业务级错误
        }
        return response;
    },
    function (error) {
        // 兼容blob下载出错json提示
        if (
            error.response &&
            error.response.data instanceof Blob &&
            error.response.data.type.toLowerCase().indexOf("json") !== -1
        ) {
            const reader = new FileReader();
            reader.readAsText(error.response.data, "utf-8");
            reader.onload = function () {
                const errorMsg = JSON.parse(reader.result).message;
                Notification.error({
                    title: "Error",
                    message: errorMsg,
                    duration: 5000,
                });
            };
        } else {
            doAppErrCode(error); //处理系统级错误
        }
        return Promise.reject(error);
    },
);
/**
 * 处理系统级错误
 */
const doAppErrCode = (error) => {
    let code = 0;
    try {
        // console.log(error.response);
        code = error.response.status;
    } catch (e) {
        if (error.toString().indexOf("Error: timeout") !== -1) {
            // Notification.error({
            //   title: "Error",
            //   message: "网络请求超时",
            // });
            return Promise.reject(error);
        }
    }
    if (code) {
        if (code === 401) {
            router.push("/");
            return Promise.reject(error);
        } else if (code === 403) {
            this.$store.state.applySuccess=false;
            Notification.error({
                title: "会话过期，请重新体验"
            });
            router.push({ path: "/" });
        } else if (code === 404) {
            const errorMsg = error.message;
            if (errorMsg !== undefined) {
                Notification.error({
                    title: errorMsg,
                });
            }
            router.push({ path: "/" });
        } else {
            const errorMsg = error.response.data.message;
            if (errorMsg !== undefined) {
                Notification.error({
                    title: errorMsg,
                });
            }
            return Promise.reject(error);
        }
    } else {
        Notification.error({
            title: "Error",
            message: "接口请求失败",
        });
        return Promise.reject(error);
    }
};
/**
 * 处理业务级错误
 */
const doBusErrCode = (response) => {
    let code = 0;
    try {
        code = response.data.returnCode;
        if (code && code != 200 && code != 9000) {
            Notification.error({
                title: "Error",
                message: response.data.message,
            });
            return response;
        }
    } catch (e) {
        return response;
    }
};
window.axios = tempAxios;
Vue.prototype.axios = tempAxios;

export default tempAxios;
